package com.itheima.controller;


import com.itheima.entity.Result;
import com.itheima.service.AdminService;
import com.itheima.utils.IpUtils;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin")
@CrossOrigin
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private RedisTemplate redisTemplate;

    private static final String USER = "user";

    @Value("${sign}")
    private String sign;

    @PostMapping("/add")
    public Result add(@RequestBody Map assignbody, HttpServletRequest request){
        String realIp = IpUtils.getRealIp(request);
        assignbody.put("lastLoginIp",realIp);
        adminService.add(assignbody);
        return new Result("success","添加用户成功");
    }

    //分配用户角色信息列表
    @PostMapping("/assign")
    public Result assign(@RequestParam("adminUid") String adminUid){
        Map map = adminService.assignByAdminUid(adminUid);
        return new Result("success",map);
    }

    @PostMapping("/delete")
    public Result delete(@RequestParam("adminUids") String[] adminUids){
        adminService.delete(adminUids);
        return new Result("success","删除用户成功");
    }

    @PostMapping("/edit")
    public Result edit(@RequestBody Map updateBody){
        adminService.edit(updateBody);
        return new Result("success","更新管理员基本信息成功");
    }

    @PostMapping("/forceLogout")
    public Result forceLogout(@RequestBody String[] tokenList){
        for (String token : tokenList) {
            Claims claims = Jwts.parser().setSigningKey(sign).parseClaimsJws(token).getBody();
            String username = claims.get("username", String.class);
            redisTemplate.boundHashOps(USER).delete(username);
        }
        return new Result("success","强制用户退出成功");
    }

    @GetMapping("/getList")
    public Result getList(@RequestParam("keyword") String keyword, @RequestParam("currentPage") Long currentPage, @RequestParam("pageSize") Long pageSize){
        Map<String,Object> map = adminService.getList(keyword,currentPage,pageSize);
        return new Result("success",map);
    }

    @GetMapping("/getOnlineAdminList")
    public Result getOnlineAdminList(@RequestParam("keyword") String keyword, @RequestParam("currentPage") Long currentPage, @RequestParam("pageSize") Long pageSize){
        List token = redisTemplate.boundHashOps("user").values();
        List list = new ArrayList();
        for (int i = 0; i < token.size(); i++) {
            if (i==(currentPage.intValue())*(pageSize.intValue())){
                break;
            }
            list.add(token.get((currentPage.intValue()-1)*(pageSize.intValue())));
        }
        Map map =new HashMap();
        map.put("records", list);
        map.put("total",token.size());
        map.put("size",pageSize);
        map.put("orders",new ArrayList<>());
        map.put("current",currentPage);
        map.put("optimizeCountSql",true);
        map.put("isSearchCount",true);
        return new Result("success",map);
    }

    @PostMapping("/restPwd")
    public Result restPassword(@RequestParam("uid") String uid){
        adminService.restPassword(uid);
        return new Result("success","重置密码成功");
    }

    @PostMapping("/updateEmail")
    public Result updateEmail(@RequestParam("uid") String uid,@RequestParam("newInfo") String newInfo,@RequestParam("validCode") String validCode){
        adminService.updateEmail(uid,newInfo, validCode);
        return new Result("success","用户信息更新成功");
    }

}
